package com.xpfirst;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Enumeration;

import com.xpfirst.R;
import com.xpfirst.socket.SocketClient;
import com.xpfirst.socket.SocketManage;

import android.*;
//客户端的实现
public class ARMClientActivity extends Activity {
	private TextView host;
	private Button load;
	private Button socketClose;
	private Button send;
	private EditText sendMsg;
	private EditText fromMsg;

	private Button goForward;
	private Button turnLeft;
	private Button autoStop;
	private Button turnRight;
	private Button fallBack;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    	ActivityManage.h = h;
        init();
        listener();
    }
    public void init(){
    	host=(TextView)findViewById(R.id.host);
        load=(Button)findViewById(R.id.load);
        socketClose = (Button)findViewById(R.id.socketClose);
        send=(Button)findViewById(R.id.send);
        sendMsg=(EditText)findViewById(R.id.sendMsg);
        fromMsg=(EditText)findViewById(R.id.fromMsg);

        goForward=(Button)findViewById(R.id.goForward);
        turnLeft=(Button)findViewById(R.id.turnLeft);
        autoStop=(Button)findViewById(R.id.autoStop);
        turnRight=(Button)findViewById(R.id.turnRight);
        fallBack=(Button)findViewById(R.id.fallBack);
    }
    private void listener(){
          load.setOnClickListener(loadOnClient);
          send.setOnClickListener(sendOnClient);
          socketClose.setOnClickListener(socketCloseOnClient);
          
          goForward.setOnClickListener(goForwardOnClient);
          turnLeft.setOnClickListener(turnLeftOnClient);
          autoStop.setOnClickListener(autoStopOnClient);
          turnRight.setOnClickListener(turnRightOnClient);
          fallBack.setOnClickListener(fallBackOnClient);
    };
    private Button.OnClickListener goForwardOnClient = new Button.OnClickListener(){
		public void onClick(View v){
			SocketManage.write("1-9");
		}
    };
    private Button.OnClickListener turnLeftOnClient = new Button.OnClickListener(){
		public void onClick(View v){
			SocketManage.write("1- ");
		}
    };
    private Button.OnClickListener autoStopOnClient = new Button.OnClickListener(){
		public void onClick(View v){
			SocketManage.write(" - ");
		}
    };
    private Button.OnClickListener turnRightOnClient = new Button.OnClickListener(){
		public void onClick(View v){
			SocketManage.write("9- ");
		}
    };
    private Button.OnClickListener fallBackOnClient = new Button.OnClickListener(){
		public void onClick(View v){
			SocketManage.write("5-11");
		}
    };
    private Button.OnClickListener socketCloseOnClient = new Button.OnClickListener(){
		public void onClick(View v){
			SocketManage.close();
		}
    };
    private Button.OnClickListener sendOnClient = new Button.OnClickListener(){
		public void onClick(View v){
			SocketManage.write("as");
		}
    };
    private Button.OnClickListener loadOnClient = new Button.OnClickListener(){
		public void onClick(View v){
			SocketManage.close();
			SocketManage.init(host.getText().toString());
		}
    };
	public Handler h=new Handler(){
        public void handleMessage(Message msg) {
        	String str;
            switch (msg.what) {
                case 0:
                	str = msg.getData().getString("socket");
                	fromMsg.setText(str);
                    break;
                case 1:
                    break;
            }
            super.handleMessage(msg);
        }
	}; 
}
